<template>
	<form-container title="新增" :width="550" :visible="visible" :destroy-on-close="true" @close="onClose">
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="编码：" name="code">
				<a-input v-model:value="formData.code" placeholder="请输入编码" allow-clear />
			</a-form-item>
			<a-form-item label="名称：" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入名称" allow-clear />
			</a-form-item>
			<a-form-item label="简称:" name="short_name">
				<a-input v-model:value="formData.short_name" placeholder="请输入简称" allow-clear />
			</a-form-item>
			<a-form-item label="联系方式:" name="phone">
				<a-input v-model:value="formData.phone" placeholder="请输入联系方式" allow-clear />
			</a-form-item>
			<a-form-item label="地址:" name="addr">
				<a-input v-model:value="formData.addr" placeholder="请输入地址" allow-clear />
			</a-form-item>
			<a-form-item label="统一社会信用代码:" name="creditCode">
				<a-input v-model:value="formData.creditCode" placeholder="请输入统一社会信用代码" allow-clear />
			</a-form-item>
			<a-form-item label="备注:" name="remark">
				<a-input v-model:value="formData.remark" placeholder="请输入备注" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="sendLoading">保存</a-button>
		</template>
	</form-container>
	<user-selector-plus ref="UserSelectorPlus" :org-tree-api="selectorApiFunction.orgTreeApi"
		:user-page-api="selectorApiFunction.userPageApi" :checked-user-list-api="selectorApiFunction.checkedUserListApi"
		@onBack="userBack" />
</template>

<script setup name="messageForm">
	import { required } from '@/utils/formRules'
	import { message } from 'ant-design-vue'
	import SupplierApi from '@/api/dev/SupplierApi.js'
	import userSelectorPlus from '@/components/Selector/userSelectorPlus.vue'
	import tool from '@/utils/tool'
	import userCenterApi from '@/api/sys/userCenterApi'
	import userApi from '@/api/sys/userApi'

	const sendLoading = ref(false)
	let UserSelectorPlus = ref()
	// 定义emit事件
	const emit = defineEmits({ successful: null })
	// 默认是关闭状态
	let visible = ref(false)
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	let userList = ref([])
	// 打开抽屉
	const onOpen = () => {
		visible.value = true
		formData.value = {}
	}
	// 关闭抽屉
	const onClose = () => {
		emit('successful')
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		code: [required('请输入编码')],
		name: [required('请输入名称')],
		shortName: [required('请输入简称')],
		phone: [required('请输入联系方式')],
		addr: [required('请输入地址')],
		creditCode: [required('请输入统一社会信用代码')],
		remark: [required('请输入备注')]
	}
	// 站内信分类字典
	const categoryOptions = tool.dictList('MESSAGE_CATEGORY')
	// 打开人员选择器
	const openUserSelector = () => {
		let ids = []
		// 打开之前选判断我们刚刚是否选过
		if (userList.value.length > 0) {
			userList.value.forEach((item) => {
				ids.push(item.id)
			})
		}
		UserSelectorPlus.value.showUserPlusModal(ids)
	}
	// 人员选择回调
	const userBack = (value) => {
		userList.value = value
	}
	// 删除某用户
	const removeUserTag = (index) => {
		userList.value.splice(index, 1)
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			sendLoading.value = true
			SupplierApi.SupplierSend(formData.value)
				.then(() => {
					message.success('新增客户成功')
					onClose()
				})
				.finally(() => {
					sendLoading.value = false
				})
		})
	}
	UserSelectorPlus.value.showUserPlusModal(ids)
}
// 人员选择回调
const userBack = (value) => {
	userList.value = value
}
// 删除某用户
const removeUserTag = (index) => {
	userList.value.splice(index, 1)
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value.validate().then(() => {
		sendLoading.value = true
		ProcessOperationApi.processOperationSend(formData.value)
			.then(() => {
				message.success('新增客户成功')
				onClose()
			})
			.finally(() => {
				sendLoading.value = false
			})
	})
}

// 传递设计器需要的API
const selectorApiFunction = {
	orgTreeApi: (param) => {
		return userApi.userOrgTreeSelector(param).then((data) => {
			return Promise.resolve(data)
		})
	},
	userPageApi: (param) => {
		return userApi.userSelector(param).then((data) => {
			return Promise.resolve(data)
		})
	},
	checkedUserListApi: (param) => {
		return userCenterApi.userCenterGetUserListByIdList(param).then((data) => {
			return Promise.resolve(data)
		})
	}
}
// 调用这个函数将子组件的一些数据和方法暴露出去
defineExpose({
	onOpen
})
</script>
